package gui;

import gui.threads.createPlayListThread;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import LogicSide.ManageSongPlaylist;

public class Playlist {

	private static String selected_playlist = "";
	private static List playlist_list;

	public static Composite createPlaylist(final Shell shell) {
		//update 
		Main.inSearch = false;
		
		final Composite playlistComposite = new Composite(shell, SWT.NONE);
		playlistComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));

		playlistComposite.setLayout(new GridLayout(2, false));

		// create a list with border, has multiple lines and has a vertical
		// scroll bar.
		playlist_list = new List(playlistComposite, SWT.BORDER
				| SWT.MULTI | SWT.V_SCROLL);
		playlist_list.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true,
				true, 1, 1));

		java.util.List<String> playlistsNames = ManageSongPlaylist
				.getPlaylists(Main.session);

		for (String playlistName : playlistsNames) {
			playlist_list.add(playlistName);
		}

		// Set list size
		Rectangle clientArea = shell.getClientArea();
		int list_width = (int) (clientArea.width / 5.0);
		int list_height = (int) (clientArea.height);
		GridData listStyle = new GridData(list_width, list_height);
		listStyle.verticalAlignment = SWT.TOP;
		playlist_list.setLayoutData(listStyle);

		// PlaylistControl
		Composite controlComposite = new Composite(playlistComposite,
				SWT.BORDER);
		controlComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		controlComposite.setLayout(new GridLayout(2, false));
		controlComposite.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_GRAY)); // set label's text color)

		// Text box displaying selection
		final Text selectedPlaylistText = new Text(controlComposite, SWT.SINGLE
				| SWT.READ_ONLY);
		selectedPlaylistText.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
				false, false, 2, 1));

		// Create playlist
		createComposite(controlComposite, shell, playlist_list);

		//

		// Table showing playlist songs
		final Table resultTable =  new Table(controlComposite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		resultTable.setLinesVisible (true);
		resultTable.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		data.horizontalSpan = 2;
		resultTable.setLayoutData(data);
		final String[] titles = new String[] {"Song"};

		for (int i=0; i<titles.length; i++) {
			TableColumn column = new TableColumn (resultTable, SWT.NONE);
			column.setText (titles [i]);
		}	
		
		for (int i=0; i<titles.length; i++) {
			resultTable.getColumn (i).pack ();
		}
		
		// add selection lisetner to playlist_list

		playlist_list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				int selected_index = playlist_list.getSelectionIndex();
				final String selected_playlist_name = playlist_list
						.getItem(selected_index);

				selectedPlaylistText.setText("Selected playlist: "
						+ selected_playlist_name);
				selected_playlist = selected_playlist_name;
				Utils.loadingCursor(Main.display, shell);
				new Thread(new Runnable() {
					
					@Override
					public void run() {
							final java.util.List<String> playlistSongs = ManageSongPlaylist.getPlaylistSongs(selected_playlist, Main.session);
							Main.display.asyncExec(new Runnable() {
								
								@Override
								public void run() {
									resultTable.clearAll();
									resultTable.setItemCount(0);
									for(String song : playlistSongs){
										TableItem item = new TableItem (resultTable, SWT.NONE);
										item.setText(song);
										System.out.println("updated playlist songs");
									}
									Utils.regularCursor(Main.display, shell);
									
								}
							});
						
						
					}
				}).start();
			}
		});
		return playlistComposite;
	}

	private static void createComposite(Composite controlComposite,
			final Shell shell, final List playlistsList) {

		// New playlist label and text widgets
		//
		Label newPlaylistLabel = new Label(controlComposite, SWT.NONE);
		newPlaylistLabel.setText("Playlist name: ");

		final Text newPlaylistText = new Text(controlComposite, SWT.SINGLE
				| SWT.BORDER);
		newPlaylistText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));

		//

		// Button for creation
		Button newPlaylistButton = new Button(controlComposite, SWT.PUSH
				| SWT.FLAT);
		newPlaylistButton.setText("Create new playlist");
		newPlaylistButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));

		newPlaylistButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (newPlaylistText.getText().length() > 0) {
					createPlayListThread thread = new createPlayListThread(
							newPlaylistText.getText(), shell, playlistsList);
					thread.start();
				}
				else{
					Utils.popUpBox("Error", "Fill playlist name!", shell);
				}
			}
		});

		// Delete playlist
		Button deletePlaylist = new Button(controlComposite, SWT.PUSH
				| SWT.FLAT);
		deletePlaylist.setText("Delete playlist");
		deletePlaylist.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));

		deletePlaylist.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (selected_playlist.length() > 0) {
					// delete selected_playlist
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							ManageSongPlaylist.deletePlaylist(selected_playlist, Main.session);
							Main.display.asyncExec(new Runnable() {
								
								@Override
								public void run() {
									playlist_list.remove(selected_playlist);
								}
							});
						}
					}).start();
				}
			}
		});
		
		
		
		//Play playlist button
		Button playButton = new Button(controlComposite, SWT.PUSH | SWT.FLAT);
		playButton.setText("Play");
		playButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));
		playButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if(selected_playlist.isEmpty())
					Utils.popUpBox("Error", "Select playlist", shell);
				else{
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							final java.util.List<String> playlistSongs = ManageSongPlaylist.getPlaylistSongs(selected_playlist, Main.session);
							Main.display.asyncExec(new Runnable() {
								
								@Override
								public void run() {
									//remove current displaying panel
									Main.current.dispose();
									//update current displaying panel
									Main.current = PlayNow.createPlayNow(shell, playlistSongs);
									//needed for it to work 
									shell.layout(true, true);
									
								}
							});
							
						}
					}).start();
				}
			}
		});
	}
}
